package cn.outsourcing.supermarket.common.code;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MP 代码生成器
 *
 * @author gzkemays
 * @since 2021/10/14 17:45
 */
public class CodeGenerator {
  public static final String PARENT_PATH = System.getProperty("user.dir") + "/src/main/java";
  public static final String AUTHOR = "gzkemays";
  /** 是否覆盖生成 */
  public static final boolean IS_OVERRIDE = false;
  /** 生成的数据库 */
  public static final String DATABASE = "mini_supermarket";
  /** 数据库端口 */
  public static final String DATA_PORT = "4097";
  /** 生成指定的表 */
  public static final String[] DATA_COLUMN = new String[] {
        //        "hiolabs_user",
        //        "hiolabs_goods",
        //        "hiolabs_goods_specification",
        //        "hiolabs_goods_gallery",
        //        "hiolabs_order_express",
        //        "hiolabs_order_goods",
        //        "hiolabs_order",
        //        "hiolabs_cart",
        //        "hiolabs_category",
        //        "hiolabs_settings",
        //        "hiolabs_show_settings",
        //        "hiolabs_ad",
        //        "hiolabs_notice",
        //        "hiolabs_specification",
        //        "hiolabs_product",
        //        "hiolabs_search_history",
        //          "hiolabs_footprint",
        //        "hiolabs_address",
        //        "hiolabs_region",
        //        "hiolabs_admin"
      };
  /** 数据连接池地址 */
  public static final String DATASOURCE_URL =
      "jdbc:mysql://127.0.0.1:"
          + DATA_PORT
          + "/"
          + DATABASE
          + "?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true";
  /** 数据库驱动 */
  public static final String DATASOURCE_DRIVER = "com.mysql.cj.jdbc.Driver";
  /** 数据库账号 */
  public static final String DATASOURCE_USER = "root";
  /** 数据库密码 */
  public static final String DATASOURCE_PWD = "a96548854";
  /** 生成的包路径 */
  public static final String PROJECT_PARENT = "cn.outsourcing.supermarket";
  /** 项目通用绝对路径 */
  private static final String PROJECT_ABSOLUTE_PATH = System.getProperty("user.dir");
  /** 绝对路径分隔符 */
  private static final String ABSOLUTE_PATH_SEPARATOR = "\\";
  /** 项目通用路径前缀 */
  private static final String PROJECT_COMMON_PREFIX =
      "\\src\\main\\java\\cn\\outsourcing\\supermarket\\";

  public static void main(String[] args) {
    AutoGenerator mpg = new AutoGenerator();
    GlobalConfig gc = new GlobalConfig();
    // 指定生成目录
    gc.setOutputDir(PARENT_PATH);
    gc.setAuthor(AUTHOR);
    gc.setOpen(false);
    gc.setFileOverride(IS_OVERRIDE);
    gc.setDateType(DateType.ONLY_DATE);
    gc.setServiceName("%sService");
    // 全局配置
    mpg.setGlobalConfig(gc);

    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setUrl(DATASOURCE_URL);
    dsc.setUsername(DATASOURCE_USER);
    dsc.setPassword(DATASOURCE_PWD);
    dsc.setDriverName(DATASOURCE_DRIVER);
    dsc.setDbType(DbType.MYSQL);
    // 数据源配置
    mpg.setDataSource(dsc);

    PackageConfig pc = new PackageConfig();
    pc.setParent(PROJECT_PARENT);
    pc.setEntity("po");
    pc.setMapper("mapper");
    pc.setService("service");
    pc.setController("controller");
    pc.setServiceImpl("service\\impl");
    String moduleName = pc.getModuleName();
    Map<String, String> pathInfo = new HashMap<>();
    // entity 路径
    pathInfo.put(
        ConstVal.ENTITY_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-service"
            + PROJECT_COMMON_PREFIX
            + pc.getEntity());
    // mapper 以及 xml 路径
    pathInfo.put(
        ConstVal.MAPPER_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-service"
            + PROJECT_COMMON_PREFIX
            + pc.getMapper());
    pathInfo.put(
        ConstVal.XML_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-service"
            + "\\src\\main\\resources\\xml\\"
            + moduleName);
    // service 和 impl 路径
    pathInfo.put(
        ConstVal.SERVICE_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-service"
            + PROJECT_COMMON_PREFIX
            + pc.getService());
    pathInfo.put(
        ConstVal.SERVICE_IMPL_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-service"
            + PROJECT_COMMON_PREFIX
            + pc.getServiceImpl());
    // controller 路径
    pathInfo.put(
        ConstVal.CONTROLLER_PATH,
        PROJECT_ABSOLUTE_PATH
            + ABSOLUTE_PATH_SEPARATOR
            + "supermarket-web"
            + PROJECT_COMMON_PREFIX
            + pc.getController());

    pc.setPathInfo(pathInfo);
    // 配置包的生成规则
    mpg.setPackageInfo(pc);

    StrategyConfig sc = new StrategyConfig();
    // 包含的表名，注释时生成全部表。
    sc.setInclude(DATA_COLUMN);
    // 下划线转换成驼峰
    sc.setColumnNaming(NamingStrategy.underline_to_camel);
    // 包的命名规则
    sc.setNaming(NamingStrategy.underline_to_camel);
    // 启用 Lombok
    sc.setEntityLombokModel(true);
    List<TableFill> tableFills = new ArrayList<>();
    TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
    TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);
    tableFills.add(createTime);
    tableFills.add(updateTime);
    sc.setTableFillList(tableFills);
    // 生成策略
    mpg.setStrategy(sc);

    mpg.execute();
  }
}
